<template>
  <div class="Order-page">
    <div class="search-field">
      <el-form :model="searchForm" inline>
        <el-form-item label="设备序列号" prop="serialNumber">
          <el-input
            placeholder="请输入设备序列号"
            style="width: 190px"
            v-model="searchForm.serialNumber"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item label="手机号码" prop="phonenumber">
          <el-input
            placeholder="请输入手机号码"
            style="width: 190px"
            v-model="searchForm.phoneNumber"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item label="创建时间" prop="createTime">
          <el-date-picker
            v-model="searchForm.createTime"
            type="daterange"
            range-separator="~"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          />
        </el-form-item>
        <el-form-item>
          <div>
            <el-button type="primary" @click="startSearch"> 查询 </el-button>
            <el-button @click="startClear"> 重置 </el-button>
          </div>
        </el-form-item>
      </el-form>
    </div>
    <div class="message-page-content">
      <div class="btns-fun-field"></div>
      <div class="table-field">
        <el-table
          :data="tableData"
          border
          stripe
          style="width: 100%"
          v-loading="tableLoading"
        >
          <el-table-column prop="id" label="id" width="190" />
          <el-table-column prop="serialNumber" label="序列号" width="140" />
          <el-table-column prop="phone" label="手机号" width="120" />
          <el-table-column prop="content" label="发送内容" min-width="320" />
          <el-table-column prop="sendStatus" label="发送状态" width="90">
            <template #default="scope">
              <span v-if="scope.row.sendStatus == 1" style="color: #c00"
                >失败</span
              >
              <span v-else style="color: #67c23a">成功</span>
            </template>
          </el-table-column>
          <el-table-column prop="reason" label="失败原因" min-width="180" />
          <el-table-column prop="createTime" label="创建时间" width="180" />
        </el-table>
      </div>
      <div class="pagination-field">
        <el-pagination
          v-model:current-page="formData.pageIndex"
          v-model:page-size="formData.pageSize"
          :page-sizes="[10, 20, 30, 40]"
          :background="true"
          layout="total, sizes, prev, pager, next, jumper"
          :total="formData.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
  </div>
</template>
<script setup lang="ts">
import dayjs from 'dayjs'
import { onMounted, reactive, ref } from 'vue'
import {
  getTextMessageList,
  getTextMessagePagination,
  getVoiceMessageList,
  getVoiceMessagePagination,
} from '@/api/message.api'
const formData = reactive({
  pageIndex: 1,
  pageSize: 10,
  total: 0,
})

const searchForm = reactive({
  serialNumber: '',
  phoneNumber: '',
  createTime: [],
})

const tableLoading = ref(false)
const tableData = ref([])

const handleSizeChange = (value: number) => {
  console.log('handleSizeChange...', value)
  formData.pageSize = value
  getDataList()
}

const handleCurrentChange = (value: number) => {
  console.log('handleCurrentChange...', value)
  formData.pageIndex = value
  getDataList()
}

const getDataList = () => {
  let searchData = {}
  if (searchForm.serialNumber) {
    searchData.serialNumber = searchForm.serialNumber
  }
  if (searchForm.phoneNumber) {
    searchData.phoneNumber = searchForm.phoneNumber
  }
  if (searchForm.createTime && searchForm.createTime.length == 2) {
    searchData.createStartTime =
      dayjs(searchForm.createTime[0]).format('YYYY-MM-DD') + ' 00:00:00'
    searchData.createEndTime =
      dayjs(searchForm.createTime[1]).format('YYYY-MM-DD') + ' 00:00:00'
  }

  tableLoading.value = true
  getTextMessagePagination({
    pageSize: formData.pageSize,
    pageIndex: formData.pageIndex,
    ...searchData,
  })
    .then(function (res: any) {
      tableLoading.value = false
      tableData.value = res.list || []
      formData.total = Number(res.total)
    })
    .catch((error) => {
      console.log(error)
      tableLoading.value = false
    })
}

onMounted(() => {
  getDataList()
})

/***
 * 开始搜索
 */
const startSearch = () => {
  let formData = {}
  if (searchForm.serialNumber) {
    formData.serialNumber = searchForm.serialNumber
  }
  if (searchForm.phoneNumber) {
    formData.phoneNumber = searchForm.phoneNumber
  }

  if (searchForm.createTime && searchForm.createTime.length == 2) {
    formData.createStartTime =
      dayjs(searchForm.createTime[0]).format('YYYY-MM-DD') + ' 00:00:00'
    formData.createEndTime =
      dayjs(searchForm.createTime[1]).format('YYYY-MM-DD') + ' 23:59:59'
  }
  getDataList(formData)
}

/***
 * 重置搜索条件
 */
const startClear = () => {
  searchForm.serialNumber = ''
  searchForm.phoneNumber = ''
  searchForm.createTime = []
  getDataList()
}
</script>
<style lang="scss" scoped>
.Order-page {
  height: 100%;
}

.btns-fun-field {
  margin-bottom: 20px;
}

.pagination-field {
  display: flex;
  justify-content: flex-end;
  margin: 20px 0;
}

.btn-inner {
  display: flex;
  justify-content: center;
  color: #fff;
}

.search-field {
  margin-bottom: 12px;
  background: #fff;
  padding: 20px 20px 0px 20px;
  border-radius: 8px;
}

.message-page-content {
  background: #fff;
  padding: 20px;
  border-radius: 8px;
}
</style>
